﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class QuanTri_NhapHSBS : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    SoLienLacDienTu cls = new SoLienLacDienTu();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
        }
        else
        {
            if (!IsPostBack)
            {
                cls.LoadddlKhoiHoc(ddlKhoi);
                cls.LoadNamHoc(ddlNam);
                cls.LoadddlBanHoc(ddlBan);
                LoadLengrvPhanBan();
                LoadLopTheoKhoiNam();
            }
        }
    }
    void LoadLopTheoKhoiNam()
    {

        var c = from p in db.LopHocs
                where p.MaKhoi == int.Parse(ddlKhoi.SelectedValue.ToString())
                select new {p.MaLop,p.TenLop};
            grvdsLop.DataSource = c;
            grvdsLop.DataBind();
    }
    void LoadLengrvPhanBan()
    {
        var c = from p in db.Lop_Bans select new {p.NamHoc.TenNam,p.LopHoc.TenLop,p.BanHoc.TenBan,p.MaLop,p.MaNam };
        grvPhanBan.DataSource = c;
        grvPhanBan.DataBind();
    }
    protected void ddlKhoi_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadLopTheoKhoiNam();
    }
    protected void ckTatCa_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < grvdsLop.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)grvdsLop.Rows[i].Cells[0].FindControl("ckItem");
            if (ckTatCa.Checked == true)
            {
                ck.Checked = true;
            }
            else
            {
                ck.Checked = false;
            }
        }
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        ddlNam.Enabled = true;
        ddlKhoi.Enabled = true;
        if (grvdsLop.Rows.Count > 0)
        {
           
            for (int i = 0; i < grvdsLop.Rows.Count; i++)
            {
                Label MaLop = (Label)grvdsLop.Rows[i].Cells[1].FindControl("lblMaLop");
                 var c=from p in db.Lop_Bans where p.MaLop==int.Parse(MaLop.Text)&&p.MaNam==int.Parse(ddlNam.SelectedValue.ToString())
                       &&p.LopHoc.MaKhoi==int.Parse(ddlKhoi.SelectedValue.ToString()) select p.MaLop;
                CheckBox ck = (CheckBox)grvdsLop.Rows[i].Cells[0].FindControl("ckItem");
                if (ck.Checked == true)
                {
                    if (c.Count() == 0)
                    {
                        Lop_Ban lb = new Lop_Ban();
                        lb.MaBan = int.Parse(ddlBan.SelectedValue.ToString());
                        lb.MaLop = int.Parse(MaLop.Text);
                        lb.MaNam = int.Parse(ddlNam.SelectedValue.ToString());
                        //lb.TrangThaiGiangDay = true;
                        db.Lop_Bans.InsertOnSubmit(lb);
                    }
                    if (c.Count() != 0)
                    {
                        Lop_Ban lb = db.Lop_Bans.SingleOrDefault(p => p.MaLop ==
                        int.Parse(MaLop.Text) && p.LopHoc.MaKhoi == int.Parse(ddlKhoi.SelectedValue.ToString())
                        && p.MaNam == int.Parse(ddlNam.SelectedValue.ToString()));
                        lb.MaBan = int.Parse(ddlBan.SelectedValue.ToString());
                    }
                }

            }
            db.SubmitChanges(); 
        }
        LoadLengrvPhanBan();
        ckTatCa.Checked = false;
    }
    protected void grvPhanBan_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = grvPhanBan.SelectedRow;
        Label MaLop = (Label)row.FindControl("lblMaLop");
        ddlKhoi.Enabled = false;
        ddlNam.Enabled = false;
        Lop_Ban lb = db.Lop_Bans.SingleOrDefault(p=>p.MaLop==int.Parse(MaLop.Text));
        ddlNam.SelectedItem.Text = lb.NamHoc.TenNam.ToString();
        ddlKhoi.SelectedItem.Text = lb.LopHoc.KhoiHoc.TenKhoi.ToString();
        ddlBan.SelectedItem.Text = lb.BanHoc.TenBan.ToString();
    }
    protected void btnSua_Click(object sender, EventArgs e)
    {

        GridViewRow row = grvPhanBan.SelectedRow;
        Label MaLop = (Label)row.FindControl("lblMaLop");
        Lop_Ban lb = db.Lop_Bans.SingleOrDefault(p=>p.MaLop==
                        int.Parse(MaLop.Text));
                        lb.MaBan = int.Parse(ddlBan.SelectedValue.ToString());
                        db.SubmitChanges();
                        LoadLengrvPhanBan();
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        GridViewRow row = grvPhanBan.SelectedRow;
        Label MaLop = (Label)row.FindControl("lblMaLop");
        Lop_Ban lb = db.Lop_Bans.SingleOrDefault(p => p.MaLop ==
                        int.Parse(MaLop.Text));
        db.Lop_Bans.DeleteOnSubmit(lb);
        db.SubmitChanges();
        LoadLengrvPhanBan();
    }
    protected void grvPhanBan_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "PhanGVCN")
        {
             for (int i = 0; i < grvPhanBan.Rows.Count; i++)
            {
            Label MaLop = (Label)grvPhanBan.Rows[i].Cells[1].FindControl("lblMaLop");
            Label MaNam = (Label)grvPhanBan.Rows[i].Cells[0].FindControl("lblMaNam");
           
                Response.Redirect("NhapGVCN.aspx?MaNam="+int.Parse(MaNam.Text)+"&MaLop="+int.Parse( MaLop.Text));
            }
        }
    }
}
